<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>奇数</title>
  </head>
  <body>
    <script>
      function toIterator(arr) {
        let i = 0;
        return {
          next() {
            while (i < arr.length) {
              const value = arr[i++];
              if (value % 2 !== 0) {
                // 只返回奇数
                return { value, done: false };
              }
            }
            return { done: true };
          },
        };
      }

      var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
      var it = {
        arr: arr,
        [Symbol.iterator]() {
          return toIterator(this.arr);
        },
      };

      for (var item of it) {
        console.log(item);
      }
    </script>
  </body>
</html>
